package ext.generic.wfaugment.processors;

import java.io.IOException;

import org.apache.poi.xssf.usermodel.XSSFSheet;

import wt.change2.WTChangeActivity2;
import wt.change2.WTChangeIssue;
import wt.change2.WTChangeOrder2;

import wt.doc.WTDocument;

import wt.epm.EPMDocument;

import wt.part.WTPart;

import wt.type.TypedUtility;

import wt.util.WTException;
import wt.util.WTProperties;

import com.ptc.core.meta.common.TypeIdentifier;

import ext.com.iba.IBAUtil;
/**
 *读取流程签审签审指南常用方法
 */
public class ReadReviewGuideUtil
{
    public static WTProperties wtProperties;
    private static boolean VERBOSE = true;
    private static String CLASSNAME = ReadReviewGuideUtil.class.getName();
    static
    {
        try
        {
            wtProperties = WTProperties.getLocalProperties();
            VERBOSE = (WTProperties.getLocalProperties()).getProperty("ext.generic.ReadReviewGuideUtil.verbose", false);
        }
        catch (Throwable throwable)
        {
            throw new ExceptionInInitializerError(throwable);
        }
    }

    /**
     *输出调试信息
     */
    private static void outDebugInfo(String info)
    {
        if (VERBOSE)
            System.out.println(CLASSNAME + "$$$$$" + info);
    }

    /**
     *读取文档签审指南所配置的该类型的行
     */
    public static int getConfigLineInGuide(WTDocument doc, XSSFSheet sheet) throws IOException, WTException
    {
        outDebugInfo("getConfigLineInGuide(WTDocument)--pbo=" + doc.getDisplayIdentity());
        // 文档大类
        String docType = WfAugmentUtil.getDocType(doc);
        outDebugInfo("	&&&&DEBUG:	docType = " + docType);
        // 文档小类
        String smallType = "";
        String wftypename = WfAugmentUtil.getPropertiesValue("ext.generic.wfaugment.wfaugment", "wf.doc.smallType");
        if (wftypename == null || wftypename.trim().length() == 0)
            smallType = "";
        else
        {
            outDebugInfo("	&&&&DEBUG:	wftypename = " + wftypename);
            smallType = (String) IBAUtil.getIBAValue(doc, wftypename);
            outDebugInfo("	&&&&DEBUG:	smallType = " + smallType);
        }
        int nSheetRow = sheet.getLastRowNum();
        // 记录实际的行
        int nAttributeRow = 0;
        // 对象的类型定义为空时，直接读取第二行
        if (smallType == null || smallType.trim().length() == 0)
            nAttributeRow = 2;
        else
        {
            // 读取大类的配置范围
            int fisrtBeginRow = 0;
            int fisrtEndRow = 0;
            // 读取第一列中大类的行范围
            for (int i = 1; i < nSheetRow; i++)
            {
                String s = WfAugmentUtil.getExcelCellContent(sheet, i, 0);
                if (s.equalsIgnoreCase(docType))
                {
                    // 记住该类的其实行
                    fisrtBeginRow = i;
                    break;
                }
            }
            // 读取到该类型的配置起始行，从该行往下读取到非空格行，做为该大类的结束行。
            // 如果是最后行，则返回最后行做为该类的结束行
            if (fisrtBeginRow != 0)
            {
                for (int i = fisrtBeginRow + 1; i < nSheetRow; i++)
                {
                    // 如果是最后行，则返回最后行做为该类的结束行
                    if (i == nSheetRow - 1)
                    {
                        fisrtEndRow = nSheetRow - 1;
                        break;
                    }
                    String s = WfAugmentUtil.getExcelCellContent(sheet, i, 0);
                    if (s.length() > 0)
                    {
                        // 记住该类的其实行
                        fisrtEndRow = i;
                        break;
                    }

                }
            }
            outDebugInfo("	&&&&DEBUG:	fisrtBeginRow = " + fisrtBeginRow);
            outDebugInfo("	&&&&DEBUG:	fisrtEndRow = " + fisrtEndRow);
            // 该类型的配置行范围内，第二列读取该小类所在的行，如果读取不到该小类，则直接读取该大类的所在行
            if (fisrtBeginRow > 0 && fisrtEndRow > 0)
            {
                // 小类属性为空，直接读取该大类所在的行的配置
                if (smallType == null || smallType.length() == 0)
                {
                    nAttributeRow = fisrtBeginRow;
                }
                else
                {
                    int secondBeginRow = 0;
                    for (int i = fisrtBeginRow; i < fisrtEndRow; i++)
                    {
                        String s = WfAugmentUtil.getExcelCellContent(sheet, i, 1);
                        outDebugInfo("	&&&&DEBUG:	getExcelCellContent = " + s);
                        if (s.length() > 0 && s.equalsIgnoreCase(smallType))
                        {
                            // 记住该小类所在行
                            secondBeginRow = i;
                            outDebugInfo("	&&&&DEBUG:	secondBeginRow = " + secondBeginRow);
                            nAttributeRow = secondBeginRow;
                            break;
                        }
                    }
                }
            }

        }
        if (nAttributeRow == 0)
        {
            nAttributeRow = 1;
        }
        outDebugInfo("	&&&&DEBUG:	nAttributeRow = " + nAttributeRow);
        return nAttributeRow;
    }

    /**
     *读取零部件签审指南所配置的该类型的行
     */
    public static int getConfigLineInGuide(WTPart part, XSSFSheet sheet) throws IOException, WTException
    {
        outDebugInfo("getConfigLineInGuide(part)--pbo=" + part.getDisplayIdentity());
        // 文档大类
        int nAttributeRow = 0;
        boolean noSmallType = false;
        String smallType = WfAugmentUtil.getPropertiesValue("ext.generic.wfaugment.wfaugment", "wf.part.smallType");
        outDebugInfo("	&&&&DEBUG:	零部件类型软属性名称 = " + smallType);
        if (smallType == null || smallType.length() == 0)
        {
            noSmallType = true;
        }
        String partAttribute = "";
        if (!noSmallType)
        {
            partAttribute = (String) IBAUtil.getIBAValue(part, smallType);
            partAttribute = partAttribute != null? partAttribute: "";
        }
        int nSheetRow = sheet.getLastRowNum();
        if (noSmallType)
        {
            outDebugInfo("	&&&&DEBUG:	系统未定义零部件类型软属性，直接读取签审指南的第二行。。。");
            nAttributeRow = 1;
        }
        else
        {
            for (int i = 1; i < nSheetRow; i++)
            {
                // 零部件类型
                String s = WfAugmentUtil.getExcelCellContent(sheet, i, 0);
                if (partAttribute.length() != 0 && s.equalsIgnoreCase(partAttribute))
                {
                    nAttributeRow = i;
                }
            }
        }
        if (nAttributeRow == 0)
        {
            nAttributeRow = 1;
            outDebugInfo("	&&&&DEBUG:	未读到该软属性的设置，直接读取签审指南的第二行。。。");
        }
        outDebugInfo("	&&&&DEBUG:	nAttributeRow = " + nAttributeRow);
        return nAttributeRow;
    }

    /**
     *读取EPM签审指南所配置的该类型的行
     */
    public static int getConfigLineInGuide(EPMDocument doc, XSSFSheet sheet) throws IOException, WTException
    {
        outDebugInfo("getConfigLineInGuide(EPMDocument)--pbo=" + doc.getDisplayIdentity());
        int nAttributeRow = 1;
        outDebugInfo("	&&&&DEBUG:	nAttributeRow = " + nAttributeRow);
        return nAttributeRow;
    }

    /**
     *读取WTChangeOrder2签审指南所配置的该类型的行
     */
    public static int getConfigLineInGuide(WTChangeOrder2 ecn, XSSFSheet sheet) throws IOException, WTException
    {

        int nAttributeRow = 0;
        int nSheetRow = sheet.getLastRowNum();
        String smallType = WfAugmentUtil.getPropertiesValue("ext.generic.wfaugment.wfaugment", "wf.ecn.smallType");
        if (smallType == null || smallType.length() == 0)
        {
            outDebugInfo("	&&&&DEBUG:	系统未定义变更通告类型软属性，直接读取签审指南的第二行。。。");
            nAttributeRow = 1;
        }
        else
        {
            String ibasmalltype = (String) IBAUtil.getIBAValue(ecn, smallType);
            if (ibasmalltype == null || ibasmalltype.trim().length() == 0)
            {
                outDebugInfo("	&&&&DEBUG:	变更通告类型软属性为空，直接读取签审指南的第二行。。。");
                nAttributeRow = 1;
            }
            else
            {
                for (int i = 1; i < nSheetRow; i++)
                {
                    String s = WfAugmentUtil.getExcelCellContent(sheet, i, 0);
                    if (s == null || s.length() == 0)
                        continue;
                    if (s.equalsIgnoreCase(ibasmalltype))
                    {
                        nAttributeRow = i;
                        break;
                    }
                }
            }
        }
        // 如果读不到对应的类型，则读取第2行
        if (nAttributeRow == 0)
        {
            nAttributeRow = 1;
            outDebugInfo("	&&&&DEBUG:	变更通告类型配置为空，直接读取签审指南的第二行。。。");
        }
        outDebugInfo("	&&&&DEBUG:	nAttributeRow = " + nAttributeRow);
        return nAttributeRow;

    }


    /**
     *读取WTChangeActivity2签审指南所配置的该类型的行
     */
    public static int getConfigLineInGuide(WTChangeActivity2 eca, XSSFSheet sheet) throws IOException, WTException
    {
        outDebugInfo(".getConfigLineInGuide(WTChangeActivity2)--eca=" + eca.getDisplayIdentity());
        String configFilePath = WfAugmentUtil.getConfigFilePath(eca);
        int nSheetRow = sheet.getLastRowNum();
        int nAttributeRow = 0; // 第1行
        String smallType = WfAugmentUtil.getPropertiesValue("ext.generic.wfaugment.wfaugment", "wf.eca.smallType");
        if (smallType == null || smallType.length() == 0)
        {
            outDebugInfo("	&&&&DEBUG:	系统未定义变更任务类型软属性，直接读取签审指南的第二行。。。");
            nAttributeRow = 1;
        }
        else
        {
            String ibasmalltype = (String) IBAUtil.getIBAValue(eca, smallType);
            if (ibasmalltype == null || ibasmalltype.trim().length() == 0)
            {
                outDebugInfo("	&&&&DEBUG:	变更任务类型软属性为空，直接读取签审指南的第二行。。。");
                nAttributeRow = 1;
            }
            else
            {
                for (int i = 1; i < nSheetRow; i++)
                {
                    String s = WfAugmentUtil.getExcelCellContent(sheet, i, 0);
                    if (s == null || s.length() == 0)
                        continue;
                    if (s.equalsIgnoreCase(ibasmalltype))
                    {
                        nAttributeRow = i;
                        break;
                    }
                }
            }
        }
        // 如果读不到对应的类型，则读取第2行
        if (nAttributeRow == 0)
            nAttributeRow = 1;
        outDebugInfo("	&&&&DEBUG:	nAttributeRow = " + nAttributeRow);
        return nAttributeRow;
    }

    /**
     *读取WTChangeIssue签审指南所配置的该类型的行
     */
    public static int getConfigLineInGuide(WTChangeIssue pr, XSSFSheet sheet) throws IOException, WTException
    {
        outDebugInfo(".getConfigLineInGuide(WTChangeIssue)--pbo=" + pr.getDisplayIdentity());
        int nAttributeRow = 0;
        int nSheetRow = sheet.getLastRowNum();
        String eciTypeitem = WfAugmentUtil.getPropertiesValue("ext.generic.wfaugment.wfaugment", "wf.eci.smallType");
        String eciType = "";
        if (eciTypeitem != null && eciTypeitem.trim().length() > 0)
            eciType = (String) IBAUtil.getIBAValue(pr, eciTypeitem);
        if (eciType == null || eciType.trim().length() == 0)
        {
            if (VERBOSE)
                System.out.println("	&&&&DEBUG:	系统未定义问题报告类型软属性，直接读取签审指南的第二行。。。");
            nAttributeRow = 1;
        }
        else
        {
            for (int i = 1; i < nSheetRow; i++)
            {
                String s = WfAugmentUtil.getExcelCellContent(sheet, i, 0);
                if (s == null || s.length() == 0)
                    continue;
                if (s.equalsIgnoreCase(eciType))
                {
                    nAttributeRow = i;
                    break;
                }
            }
        }

        // 如果读不到对应的类型，则读取第2行
        if (nAttributeRow == 0)
            nAttributeRow = 1;
        outDebugInfo("	&&&&DEBUG:	nAttributeRow = " + nAttributeRow);
        return nAttributeRow;
    }

    public static void main(String args[])
    {
        System.exit(0);
    }

}
